#include <iostream>
#include "parser.h"

using namespace std;

Parser::Parser()
{
    init();
}

// Should student be a part of parser or of the outside program?
Parser::Parser( const string& studentFile  )
    :studentFilePath(studentFile)
{
    
    init();

    cout << "Reading student from: " << studentFile << endl;

    
}
bool Parser::init()
{
    pStudent = new Student();
    // pvCourses = new vector<Course> ();

    return true;
}
Parser::~Parser()
{
    if(pStudent)
		delete pStudent;
    /*
	  if(pvCourses)
	  delete pvCourses;
    */
}
bool Parser::parse()
{

    try
	{
	    
	    parser.set_substitute_entities(); //&entities; -> "value"
	    parser.parse_file(studentFilePath);
	    	    
	}
    catch(const std::exception& ex)
	{
	    cerr << "Exception caught: " << ex.what() << endl;
	    return false;
	}

    return true;
}

Student* Parser::getStudent() const
{

    return pStudent;
}

bool Parser::doStudent()
{
    if(!parser)
		return false;
		
		    
    document = parser.get_document();
    xmlpp::Element* pNode = document->get_root_node();
    
    
    // pNode = root of document.
    // Load the student with his info.
    pStudent->setFirstName( pNode->get_attribute("first")->get_value() );
    pStudent->setLastName( pNode->get_attribute("last")->get_value() );
    pStudent->setStudentID( pNode->get_attribute("id")->get_value() );
    
    pStudent->setPassword( pNode->get_attribute("password")->get_value() );
    pStudent->setCurriculumFile( pNode->get_attribute("curriculum")->get_value() );
    return true;
}

bool Parser::doCourses()
{
	return true;
}

bool Parser::doCurriculum()
{
	return true;
}
